<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="门店名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="店长手机号码" prop="storeUserPhone">
        <el-input
          v-number-input
          v-model="queryParams.storeUserPhone"
          placeholder="请输入"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="openDialog"
          >新增门店</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="roleList">
      <el-table-column label="门店名称" prop="storeName" />
      <el-table-column
        label="店长手机号码"
        prop="storeUserPhone"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="门店地址" prop="storeAddress" />
      <el-table-column label="更新人" prop="updateByName" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="createTime"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <div class="operate-box">
            <el-button type="text" @click="openDialog('edit', scope.row)"
              >编辑</el-button
            >
            |
            <el-button type="text" @click="handleRemove(scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="dialogTitle"
      :visible.sync="addOrUpdateDialog"
      width="600px"
      append-to-body
      custom-class="custom-dialog" :close-on-click-modal="false"
    >
      <el-form
        ref="dialogFormRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="门店名称：" prop="storeName">
          <el-input
            v-model="form.storeName"
            placeholder="请输入门店名称"
            maxlength="20"
          />
        </el-form-item>
        <el-form-item label="店长手机号码" prop="storeUserPhone">
          <el-input
            v-number-input
            maxlength="11"
            v-model="form.storeUserPhone"
            placeholder="请输入手机号码"
          />
        </el-form-item>
        <el-form-item label="店长登录密码：" prop="storePwd">
          <el-input
            v-model="form.storePwd"
            placeholder="请输入密码"
            minlength="6"
          />
        </el-form-item>
        <el-form-item label="门店地址：" prop="storeAddress">
          <el-input
            v-model="form.storeAddress"
            maxlength="50"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('dialogFormRef')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addStore,
  updateStore,
  getStoreList,
  delstore,
} from "@/api/system/store";

const validatePassword = (rule, value, callback) => {
  const regex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{6,}$/;
  if (!regex.test(value)) {
    callback(new Error("店长登录密码必填，至少为6位字母或数字组合"));
  } else {
    callback();
  }
};

export default {
  name: "StoreInformation",
  data() {
    return {
      loading: false,
      ids: [],
      dateRange: [],
      roleList: [],
      total: 1,
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        storeName: undefined,
        storeUserPhone: undefined,
      },
      addOrUpdateDialog: false,
      dialogTitle: "",
      form: {
        storeName: "",
        storeUserPhone: "",
        storePwd: "",
        storeAddress: "",
      },
      rules: {
        storeName: [
          {
            required: true,
            message: "门店名称必填，限输入20个字",
            trigger: "blur",
          },
        ],
        storeUserPhone: [
          {
            required: true,
            message: "手机号码不能为空",
            trigger: "blur",
          },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        storePwd: [
          {
            required: true,
            validator: validatePassword,
            trigger: "blur",
          },
        ],
        storeAddress: [
          {
            required: true,
            message: "门店地址必填，限输入50个字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getStoreList(this.queryParams).then((response) => {
        this.roleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleRemove(row) {
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delstore(row.id).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    openDialog(type, row) {
      this.dialogTitle = type === "edit" ? "编辑门店" : "新增门店";
      this.addOrUpdateDialog = true;
      if (type === "edit") {
        this.form = { ...row };
      } else {
        this.form = {
          storeName: "",
          storeUserPhone: "",
          storePwd: "",
          storeAddress: "",
        };
      }
    },
    closeDialog() {
      this.addOrUpdateDialog = false;
      this.resetForm("dialogFormRef");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid, errorObj) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateStore(this.form).then((res) => {
              if (res.code === 200) {
                this.$message({
                  type: "success",
                  message: "编辑成功!",
                });
                this.getList();
                this.closeDialog();
              }
            });
          } else {
            addStore(this.form).then((res) => {
              if (res.code === 200) {
                this.$message({
                  type: "success",
                  message: "新增成功!",
                });
                this.getList();
                this.closeDialog();
              }
            });
          }
        }
      });
    },
  },
};
</script>
